<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
	<base href="<%=basePath%>">
	<title>接口监控</title>
<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
	<script language="javascript" type="text/javascript">
		$(document).ready(function() {
			queryAppname();
			
			$('#channelSelect').change(function(){
				var appid = $('#channelSelect option:selected').attr('name');
				if(appid==undefined){
					$('#serviceSelect option:gt(0)').remove();
				}else{
					loadServiceSelect(appid);				
				}
			});
		});
		function queryAppname(){
			
			$.ajax({
				url : "monitorManager/queryAppname",
				type : 'POST',
				async : true,
				dataType : 'json',
				success : function(obj) {
					if($.validate(obj))return;
					var list = obj.data;
					var appSelect = document.getElementById("channelSelect"); 
					appSelect.options.length = 0;
					appSelect.add(new Option("--请选择--", ""));
					var dataLength = 0;
					if(list != null){
						dataLength = list.length;
					}
					for(var i=0;i<dataLength;i++)
  					{
  						var option=new Option();
						option.text=list[i].cacheName;
						option.value=list[i].connindex;
						option.setAttribute("name", list[i].appId);
						appSelect.add(option);
  					}
				}
			});
		}
		function loadServiceSelect(appid){
			$.ajax({
					url : "monitorManager/queryServiceIp",
					type : 'POST',
					async : true,
					data : {appId:appid},
					dataType:'json',
					success : function(obj) {
						if($.validate(obj))return;
						var list = obj.data;
						var appSelect = document.getElementById("serviceSelect"); 
						appSelect.options.length = 0;
						appSelect.add(new Option("--请选择--", ""));
						var dataLength = 0;
						if(list != null){
							dataLength = list.length;
						}
						for(var i=0;i<dataLength;i++)
      					{
      						var option=new Option();
							option.text=list[i].serviceIp;
							option.value=list[i].serviceIp;
							appSelect.add(option);
      					}
					}
				});
		}

		//加载缓存管理列表
	 	function cacheDateLoad(param){
				$.ajax({
					url : "monitorManager/cacheInfoList",
					type : 'POST',
					async : true,
					data: param,
					dataType : 'json',
					success : function(obj) {
						if($.validate(obj))return;
						var buttonStr="<input type='button'  class='input_button_1'  id='btnclearchanne' value='清除' onClick=btnclear(this,'"+param.monitorServiceIndex+"') />";
 						var divContent = creatTable1("cacheindex@TDH,cacheName",obj,null,buttonStr);
 						
 						$("#tbcachemanager").find("tbody").remove(); 
 						$("#tbcachemanager").append(divContent);
 						//加载连接器管理
				        //connmanagerLoad(ifindex,serviceIp);	
					}
				});
		}
			
			//点击清理缓存按钮(参数为点击要清理的缓存对象)
			function btnclear(objcache,ifindex){
				var serviceIp=$("#serviceSelect").val();
				var cacheindex=objcache.parentElement.parentElement.childNodes[0].innerHTML;
				if(serviceIp==""){
					$.message("error","请选择你要清除的服务器地址");
					return;
				}
				var param={monitorServiceIndex:ifindex,serviceIp:serviceIp,cacheindex:cacheindex};
				if (confirm("确定要执行此操作吗？")) {
					$.ajax({
							url : "monitorManager/clearCacheInfoManager",
							type : 'POST',
							async : true,
							data: param,
							dataType : 'json',
							success : function(obj) {
								if($.validate(obj))return;
							}
					});
				 }
			} 	
			
			function querydata(){
				var ifindex = $("#channelSelect").val();
				if(ifindex==""){
					$.message("error","请选择你要查询的网关接口");
					return;
				}
				var serviceIp = $("#serviceSelect").val();
				if(serviceIp==""){
					$.message("error","请选择你要查询的服务器地址");
					return;
				}
				var param={monitorServiceIndex:ifindex,serviceIp:serviceIp};
				cacheDateLoad(param);
				connmanagerLoad(param);
				logDateLoad(param);
				divManager.style.display='block';
			}
			function connmanagerLoad(param){
				$.ajax({
					url : "monitorManager/connStatusList",
					type : 'POST',
					async : true,
					data: param,
					dataType : 'json',
					success : function(obj) {
						if($.validate(obj))return;
						var buttonStr="<input type='button' name='btstarted' value='开启'    class='input_button_1' id='btnstartconn' onClick=operationConn(this,'"+param.monitorServiceIndex+"','"+param.serviceIp+"')> <input type='button' name='butstop' value='关闭' class='input_button_1'  onClick=operationConn(this,'"+param.monitorServiceIndex+"','"+param.serviceIp+"')>";
 						var divContent = creatTable1("connindex@TDH,connStauts@TDH,connName,connStauts?[{started:'开启'};{stopped:'关闭'}]",obj,null,buttonStr);
 						$("#tbconnmanager").find("tbody").remove(); 
 						$("#tbconnmanager").append(divContent);
 						
					}
				});
			}
			function operationConn(obj,index,serviceIp){
				if(obj.parentElement.parentElement.childNodes.length!=0){
					 var strconninfo=obj.parentElement.parentElement;
					 //连接器序列
					 var str_connindex= strconninfo.childNodes[0].innerHTML;
					 //连接器状态
					 var str_connstatus= strconninfo.childNodes[1].innerHTML;
					 
					 var clickbutvalue=obj.value;
					  if(clickbutvalue=='开启' && str_connstatus=='started'){
					   $.message("error","连接器已经"+clickbutvalue);
					 	return false;
					 }
					 if(clickbutvalue=='关闭' && str_connstatus=='stopped'){
					   $.message("error","连接器已经"+clickbutvalue);
					   return false;
					 }
					 
					 
					 if (confirm("确定要执行此操作吗？")) { 
				      	 $.ajax({
							url : "monitorManager/operatConn",
							type : 'POST',
							async : true,
							data: {monitorServiceIndex:index,serviceIp:serviceIp,connindex:str_connindex,connstatus:str_connstatus},
							dataType : 'json',
							success : function(obj) {								
							if($.validate(obj))return;
							connmanagerLoad({monitorServiceIndex:index,serviceIp:serviceIp});
							}
						}); 
				    }
				}	
			}
			function logDateLoad(param){
				//加载日志管理
					$.ajax({
					url : "monitorManager/logManagerList",
					type : 'POST',
					async : true,
					data:param,
					dataType : 'json',
					success : function(obj) {
						var buttonStr= "<select id='selevel' name='selevel'  ><option value='请选择'>--请选择--</option><option value='INFO'>INFO</option><option value='DEBUG'>DEBUG</option><option value='WARN'>WARN</option><option value='ERROR'>ERROR</option><option value='TRACE'>TRACE</option></select>&nbsp;&nbsp; <input type='button' name='butstop' value='修 改' class='input_button_1'  onClick=updatelogstautus(this,'"+param.monitorServiceIndex+"','"+param.serviceIp+"')>";
					    var str_tr="<tr><td><input  type='text' id='txtlogname'  style='width:380px'/></td><td><select id='selevel' style='width:100px' name='selevel' ><option value='请选择'>--请选择--</option><option value='INFO'>INFO</option><option value='DEBUG'>DEBUG</option><option value='WARN'>WARN</option><option value='ERROR'>ERROR</option><option value='TRACE'>TRACE</option></select></td><td><input type='button' class='input_button_1' name='butstop' onClick=addlogname(this,'"+param.monitorServiceIndex+"','"+param.serviceIp+"') value='新 增'  />&nbsp;&nbsp;<input type='button' name='butstop'  class='input_custombutton' onClick=resetOperation('"+param.monitorServiceIndex+"','"+param.serviceIp+"') value='还原默认配置'/></td></tr>";
						var divContent = creatTable1("logName,logLeven",obj,null,buttonStr);
						$("#tblogmanager").find("tbody").remove(); 
						$("#tblogmanager").append(divContent);
						$("#tblogmanager").append(str_tr);
						 //加载缓存管理列表
						}
				}); 
			}
			//修改日志级别
			function updatelogstautus(obj,ifindex,serviceIp){
			
				 var strconninfo=obj.parentElement.parentElement;
				 //日志名称
				 var str_logname= strconninfo.childNodes[0].innerHTML;
				 //原始日志级别
				 var old_loglevel= strconninfo.childNodes[1].innerHTML;
				 
				 //选中的日志级别
				 var str_loglevel= strconninfo.childNodes[2].firstChild.value;
				 if(str_loglevel=='请选择')
				 {
					 $.message("error","请选择要修改日志的级别！");
					 return false;
				 }
				 if(str_loglevel==old_loglevel)
				 {
				 	$.message("error","当前的日志级别已经是'"+old_loglevel+"'请重新设置！");
				 	return false;
				 }
				 var param={monitorServiceIndex:ifindex,serviceIp:serviceIp,logName:str_logname,logLeven:str_loglevel};
				 updateLogLeven(param);	
			}
			//重置操作
			function resetOperation(ifindex,serviceIp){
				var param={monitorServiceIndex:ifindex,serviceIp:serviceIp};
				
				
				$.dialog({
					type: "warn",//success,error
					content: "确定要还原默认配置吗？",
					onOk: function() {
						resetLog(param);
					}
				});
			}	
			function resetLog(param){
				$.ajax({
					url : "monitorManager/resetLog",
					type : 'POST',
					async : true,
					 data: param,
					dataType : 'json',
					success : function(obj) {
						if($.validate(obj))return;
							//刷新日志管理器页面
						logDateLoad(param);
					}
				});
			}
			//新增日志级别
			function addlogname(addobj,ifindex,serviceIp){
					//文本框中输入的日志名称
					 var txtlogname=$("#txtlogname").val();
					//当前按钮点击的值
					var clickbtnval=addobj.defaultValue;
					 //选中的日志级别
					 var loglevel= addobj.parentElement.parentElement.childNodes[1].firstChild.value;
					
					 if(txtlogname=="")
					 {
						$.message("error","请填写要新增的日志名称");
						return false;
					 }
					 if(loglevel=='请选择')
					 {
						 $.message("error","请选择要新增日志的级别");
						 return false;
					 }
					var param={monitorServiceIndex:ifindex,serviceIp:serviceIp,logName:txtlogname,logLeven:loglevel};
					updateLogLeven(param);	
				}
			function updateLogLeven(param){
				$.ajax({
					url : "monitorManager/updateLogLeven",
					type : 'POST',
					async : true,
					data: param,
					dataType : 'json',
					success : function(obj) {
						if($.validate(obj))return;
						logDateLoad(param);
						
					}
				});
			}
	</script>
 </head>
 <body>
<div class="current">
           <table class="current_table_1">
              <tbody>
                <tr class="f2">
                  <td>
                    <img src="./images/current_1.jpg">
                  </td>
                  <td class="current_table_1_td">
					<img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
					<span style=" color:#136bab; ">应用管理
					<span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>应用监控
                  </span>
                  </td>
                  <td><img src="./images/current_3.jpg"></td>
              </tr>
           </tbody>
         </table>
</div>
 	<table id="transTable" style='width:99%;text-align:center'>
 	<tr>
 		<td>
 			<div id="transcountDiv" >
				<table class="table_02" id="transcountTb1" style='height:49px;width:99%;font-family:"宋体"; font-size:14px;'>
					 <tr>
					 	<td colspan="2" align="center">
					 		网关应用:<select class="search_select_1" id="channelSelect">
					            		<option value="-1">--请选择--</option>
									  </select>&nbsp;&nbsp;&nbsp;&nbsp;
							服务器:	  <select class="search_select_1" id="serviceSelect">
					            		<option value="">--请选择--</option>
									  </select>&nbsp;&nbsp;
							<input type="button" class="input_button_1" value="查询" onclick="querydata();">
					 	</td>
					 </tr>
				</table>
			</div>
 		</td>
 	</tr>
 	<tr>
 		<td>
				<div id="divManager" style="display: none;">
				<table id="transcountTb2" class="table_10"  style='width:98%;font-family:"宋体"; font-size:14px;'>
					 <tr>
						  <td width="60%">
									<table id="tbconnmanager" class="table_11" >
									 	<thead>
										 	<tr>
								        		<td  colspan="3"><span class="title_1">连接器管理</span></td>
								      		</tr>
											  <tr>
											    <th style="width:34%">连接器</th>
											    <th style="width:34%">状态</th>
											    <th style="width:32%">操作</th>
							                </tr>
							             </thead>   
							   	   </table>	
							</td>
							<td width="40%">
									<table  id="tbcachemanager"  class="table_11">
										<thead>
											<tr>
									        	<td  colspan="2"><span class="title_1">缓存管理</span></td>
									      	</tr>
											  <tr>
											    <th style="width:50%">缓存对象</th>
											    <th style="width:50%">操作</th>
											  </tr>
										</thead>	  
								     </table>
							</td>
					  </tr>
					  <tr>
					    <td colspan="2" >
							<table id="tblogmanager"  class="table_02">
							<thead>
								<tr>
							        <td  colspan="3"><span class="title_1">日志管理</span></td>
							      </tr>
							       <tr>
							        <th style="width:34%">日志名</th>
							        <th style="width:34%">日志级别</th>
							        <th style="width:32%">操作</th>
							      </tr>
							</thead>
							      <tbody>
							      </tbody>
						    </table>
					   </td>
					 </tr>
				</table>
			</div>

 		</td>
 	</tr>
 	
	</table>
 </body>
</html>
		